package com.lzm.flash;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by lzm on 2018/2/26.
 *
 * 联系人页面
 */

public class MainContactPage extends Fragment{

    private class NumberData
    {
        public String name;
        public String ip;
        public String mac;

        public NumberData(String name,String ip,String mac)
        {
            this.ip=ip;
            this.name=name;
            this.mac=mac;
        }
    }

    private RefreshListView m_numberList;
    private ArrayList<NumberData> m_numberData=new ArrayList();
    private NumberListViewAdapter m_numberListAdapter;


    public static MainContactPage newInstance() {

        Bundle args = new Bundle();

        MainContactPage fragment = new MainContactPage();
        fragment.setArguments(args);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.number_list_view,container,false);
        return view;
    }
    //onCreateView 之后被调用
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        m_numberList = (RefreshListView) view.findViewById(R.id.number_list);
        m_numberListAdapter=new NumberListViewAdapter();
        //设置适配器
        m_numberList.setAdapter(m_numberListAdapter);
        //设置刷新接口
        m_numberList.setInterface(new refreshData());

        //监听item 点击事件
        m_numberList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                intent.setClass(getActivity(),MsgActivity.class);
                NumberData numberData=((NumberData)m_numberListAdapter.getItem(i-1));
                intent.putExtra("name",numberData.name);
                intent.putExtra("mac",numberData.mac);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    //刷新数据的接口
    private class refreshData implements RefreshListView.IRefreshListener{

        @Override
        public void onRefresh() {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    m_numberList.refreshComplete();
                }
            }, 2000);
            return;
        }
    }

    //联系人列表的分配器
    private class NumberListViewAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            return m_numberData.size();
        }

        @Override
        public Object getItem(int i) {

            return m_numberData.get(i);
        }

        @Override
        public long getItemId(int i) {

            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if (view==null)
            {
                //注意    在fragment 中需要先获取 activity
                view=getActivity().getLayoutInflater().inflate(R.layout.number_item,viewGroup,false);
            }

            NumberData data=(NumberData) getItem(i);

            TextView name=(TextView)view.findViewById(R.id.name);
            name.setText(data.name);
            TextView ip=(TextView)view.findViewById(R.id.ip);
            ip.setText(data.ip);

            return view;
        }
    }
    /*
    * 更新成员列表数据
    */
    public void updataNumberListData(Bundle data){
        m_numberData.add(new NumberData(data.getString("name")
                ,data.getString("ip"),data.getString("mac")));
        m_numberListAdapter.notifyDataSetChanged();
    }
    interface IContactPageInterface{
        //void updataNumberList();
    }
}
